package com.oocl.im.handler.impl;

import com.oocl.im.entity.Client;
import com.oocl.im.constants.ServerConstants;
import com.oocl.im.constants.ServerMap;
import com.oocl.im.entity.Protocol;
import com.oocl.im.handler.IHandle;
import com.oocl.im.outputstream.impl.NetOutput;

import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;

public class ChatHandle implements IHandle {
    ObjectOutputStream oos;

    public ChatHandle(ObjectOutputStream oos) {
        this.oos = oos;
    }

    @Override
    public void handle(Client client, Protocol protocol) {
        System.out.println("here is in private chat handle");
        Client to = (Client) protocol.getParams().get(ServerConstants.TO);
        System.out.println(to.getAccount());
        if ("@all".equals((String)to.getAccount()))
        {
            System.out.println("in if :"+to.getAccount());
            new GroupChatHandle(oos).handle(to,protocol);
        }else {
            protocol.setMsgType(ServerConstants.RECHAT);
            ObjectOutputStream toOos = ServerMap.socketMap.get(to);


            Map<String, Object> resParams = new HashMap<>();
            resParams.put(ServerConstants.STATUS, ServerConstants.SUCCESS);
            Protocol res = new Protocol(ServerConstants.RESPONSE, resParams);

            try {
                NetOutput tmpOut = new NetOutput(toOos);
                tmpOut.write(protocol);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }


}
